package com.aube.mis.service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.apiv2.service.admin.push.PushMessageAdminVoService;
import com.aube.apiv2.vo.push.PushPageVo;
import com.aube.apiv2.vo.push.msg.req.PushMessageReqVo;
import com.aube.apiv2.vo.push.msg.resp.PushMessageRespVo;
import com.aube.apiv2.vo.push.msg.resp.PushPageRelateDataVo;
import com.aube.apiv2.vo.push.msg.resp.PushResultRespVo;
import com.aube.apiv2.vo.push.query.PushMsgQueryVo;
import com.aube.common.req.vo.PaginationReqVo;
import com.aube.common.resp.vo.PaginationRespVo;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.PushDTO;
import com.aube.mis.dto.PushRelatedTagDTO;
import com.aube.mis.dto.PushResultDTO;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.PushService;
import com.aube.mis.vo.DataTableCriterias;
import com.aube.support.ResultCode;

@Service
@SuppressWarnings("unchecked")
public class PushServiceImpl extends AbstractService implements PushService {

	private static Logger log = LoggerFactory.getLogger(PushServiceImpl.class);

	@Resource
	PushMessageAdminVoService pushMessageAdminVoService;

	@Override
	public PaginationResultDTO<PushDTO> getPushList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<PushMsgQueryVo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		Map<String, Boolean> sortMap = new HashMap<String, Boolean>();
		sortMap.put("id", false);
		reqVo.setSortMap(sortMap);
		ResultCode<PaginationRespVo<PushMessageRespVo>> serverResp = pushMessageAdminVoService.queryList(reqVo);
		final ResultCode<List<PushPageVo>> pushPageVoList = pushMessageAdminVoService.getPushPageVo();
		List<PushDTO> resultList = (List<PushDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public PushDTO transform(Object object) {
				PushDTO result = convertPushDTO((PushMessageRespVo) object);
				for (PushPageVo pushPageVo : pushPageVoList.getRetval()) {
					if(pushPageVo.getRelatedTag().equals(result.getRelatedTag())) {
						result.setRelatedDesc(pushPageVo.getRelatedDesc());
					}
				}
				return result;
			}
		});
		PaginationResultDTO<PushDTO> result = new PaginationResultDTO<PushDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	@Override
	public void editPush(PushDTO pushDTO) {
		PushMessageReqVo pushMsgVo = new PushMessageReqVo();
		pushMsgVo.setAliasIds(pushDTO.getAliasIds());
		pushMsgVo.setContent(pushDTO.getAndroidContent());
		pushMsgVo.setCategoryId(pushDTO.getCategoryId());
		pushMsgVo.setIosContent(pushDTO.getIosContent());
		pushMsgVo.setOsType(pushDTO.getOsType());
		if(StringUtils.isNotEmpty(pushDTO.getPushId())) {
			pushMsgVo.setId(Long.parseLong(pushDTO.getPushId()));
		}
		pushMsgVo.setRelatedId(pushDTO.getRelatedId());
		pushMsgVo.setRelatedTag(pushDTO.getRelatedTag());
		pushMsgVo.setSendEnv(pushDTO.getSendEnv());
		pushMsgVo.setShowid(Long.parseLong(pushDTO.getShowId()));
		pushMsgVo.setSendTime(new Timestamp(pushDTO.getSendTime()));
		pushMsgVo.setTitle(pushDTO.getTitle());
		pushMessageAdminVoService.saveOrModify(pushMsgVo);
	}

	@Override
	public PushDTO getPushDetail(String pushId) {
		ResultCode<PushMessageRespVo> result = pushMessageAdminVoService.getDetailById(Long.parseLong(pushId));
		return convertPushDTO(result.getRetval());
	}
	
	@Override
	public PushPageRelateDataVo getPushRelateData(String showId, String relatedTag) {
		ResultCode<PushPageRelateDataVo> result = pushMessageAdminVoService.getPushPageRelateDataVo(Long.parseLong(showId), relatedTag);
		return result.getRetval();
	}
	
	@Override
	public List<PushRelatedTagDTO> getPushRelatedTagList() {
		ResultCode<List<PushPageVo>> serverResp = pushMessageAdminVoService.getPushPageVo();
		List<PushRelatedTagDTO> resultList = (List<PushRelatedTagDTO>) CollectionUtils.collect(serverResp.getRetval(), new Transformer() {

			@Override
			public PushRelatedTagDTO transform(Object object) {
				PushPageVo vo = (PushPageVo) object;
				PushRelatedTagDTO result = new PushRelatedTagDTO();
				result.setRelatedTag(vo.getRelatedTag());
				result.setRelatedDesc(vo.getRelatedDesc());
				return result;
			}
		});
		return resultList;
	}
	
	@Override
	public void deletePush(String pushId) {
		pushMessageAdminVoService.removeById(Long.parseLong(pushId));
	}

	private PushDTO convertPushDTO(PushMessageRespVo vo) {
		PushDTO result = new PushDTO();
		result.setAliasIds(vo.getAliasIds());
		result.setAndroidContent(vo.getContent());
		result.setCategoryId(vo.getCategoryId());
		result.setIosContent(vo.getIosContent());
		result.setOsType(vo.getOsType());
		result.setPushId(vo.getId().toString());
		result.setPushStatus(vo.getPushStatus());
		result.setRelatedId(vo.getRelatedId());
		result.setRelatedTag(vo.getRelatedTag());
		result.setSendEnv(vo.getSendEnv());
		result.setShowId(vo.getShowid().toString());
		result.setSendTime(vo.getSendTime().getTime());
		result.setTitle(vo.getTitle());
		List<PushResultRespVo> pushResultRespVoList = vo.getPushResultRespVoList();
		if(CollectionUtils.isNotEmpty(pushResultRespVoList)) {
			List<PushResultDTO> resultList = (List<PushResultDTO>) CollectionUtils.collect(pushResultRespVoList, new Transformer() {

				@Override
				public PushResultDTO transform(Object object) {
					PushResultRespVo vo = (PushResultRespVo) object;
					PushResultDTO result = new PushResultDTO();
					try {
						BeanUtils.copyProperties(result, vo);
					} catch (Exception e) {
						e.printStackTrace();
					}
					return result;
				}
			});
			result.setResultList(resultList);
		}
		return result;
	}

}
